define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiservice/wxtemplate/index' + location.search,
                    add_url: 'xiservice/wxtemplate/add',
                    edit_url: 'xiservice/wxtemplate/edit',
                    del_url: 'xiservice/wxtemplate/del',
                    multi_url: 'xiservice/wxtemplate/multi',
                    import_url: 'xiservice/wxtemplate/import',
                    table: 'xiservice_wxtemplate',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},
                        //{field: 'event', title: __('Event'), operate: 'LIKE'},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'template_id', title: __('Template_id'), operate: 'LIKE'},
                        {field: 'page_url', title: __('Page_url'), operate: 'false'},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"0":__('Status 0')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(document).on("fa.event.appendfieldlist", '.btn-append', function(){
                //通用的表单组件事件绑定和组件渲染
                  Form.events.selectpage($(".fieldlist"));
                  Form.events.selectpicker($(".fieldlist"));
            });
            Controller.api.bindevent();
        },
        edit: function () {
            $(document).on("fa.event.appendfieldlist", '.btn-append', function(){
                //通用的表单组件事件绑定和组件渲染
                  Form.events.selectpage($(".fieldlist"));
                  Form.events.selectpicker($(".fieldlist"));
            });

            $(document).on('change keyup changed', ".json textarea", function () {
                // 更新数据
                $(".fieldlist").trigger("fa.event.refreshfieldlist");

                let op = $(".json option");
                for (let i=0;i<op.length;i++){
                    //console.log(op.eq(i).attr("sex"));
                    if(op.eq(i).attr("type") == op.eq(i).val()){
                        op.eq(i).attr("selected",true);
                    }
                }
                // 重新渲染下拉框
                Form.events.selectpicker($("form[role=form]"));
            });
            setTimeout(function (){
                $(".json textarea").trigger("change");
            },100);          
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
